﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

class DiskDatabase : CustomDatabase
{
    public DiskDatabase()
    {

    }

    public void storeCustomer(Customer customer)
    {
        Customer copy = (Customer)customer.clone();
        
        using (StreamWriter w = File.AppendText("customers.txt"))
        {
            Log(copy.getNume() + " " + copy.getComputer() + " " + copy.getProcNume() + " " + copy.getRAMNume() + " " + copy.getMonitorNume(), w);
        }
    }

    private static void Log(string logMessage, TextWriter w)
    {
        w.WriteLine(logMessage);
    }


    public void printCustomers()
    {
        string line;

        System.IO.StreamReader file = new System.IO.StreamReader("customers.txt");
        while ((line = file.ReadLine()) != null)
        {
            string[] words = line.Split(' ');
            Console.WriteLine(words[0]);

            if(words[1] == "0")
                Console.Write("ServerComputer: ");
            else
                if (words[1] == "1")
                    Console.Write("HomeComputer: ");
                else
                    Console.Write("GamingComputer: ");

            Console.WriteLine(words[2] + " " + words[3] + " " + words[4]);
        }

        file.Close();
    }


    public void deleteCustomer(Customer client)
    {
        string line;
        int tip_calculator, gasit;
        System.IO.StreamWriter outputFile = new System.IO.StreamWriter("out.txt");
        System.IO.StreamReader file = new System.IO.StreamReader("customers.txt");

        while ((line = file.ReadLine()) != null)
        {
            gasit = 0;
            string[] words = line.Split(' ');

            tip_calculator = Convert.ToInt32(words[1]);
            if ((client.getNume() == words[0]) && (client.getComputer() == tip_calculator) && (client.getProcNume() == words[2]) && (client.getRAMNume() == words[3]) && (client.getMonitorNume() == words[4]))
            {
                gasit = 1;
            }

            if (gasit == 0)
            {
                outputFile.WriteLine(words[0] + " " + words[1] + " " + words[2] + " " + words[3] + " " + words[4]);
            }
        }

        outputFile.Close();
        file.Close();

        File.Replace("out.txt", "customers.txt", "backup.txt");
        File.Delete("backup.txt");
    }
}
